libostree: Move prune into OstreeRepo namespace
authorColin Walters <walters@verbum.org>
Wed, 10 Jul 2013 00:05:31 +0000 (20:05 -0400)
committerColin Walters <walters@verbum.org>
Wed, 10 Jul 2013 00:05:31 +0000 (20:05 -0400)
More library work.

Makefile-libostree.am
Makefile-ostree.am
src/libostree/ostree-repo-prune.c [new file with mode: 0644]
src/libostree/ostree-repo.h
src/ostree/ostree-prune.c [deleted file]
src/ostree/ostree-prune.h [deleted file]
src/ostree/ot-admin-cleanup.c
src/ostree/ot-admin-functions.c
src/ostree/ot-builtin-prune.c

index 3de3469bbcc728b5f28a5457fc8ea063354c72e8..3e5bce64492dc22b9b813b96507b7c52ca66227a 100644 (file)
@@ -33,6 +33,7 @@ libostree_la_SOURCES = src/libostree/ostree.h \
        src/libostree/ostree-repo.c \
        src/libostree/ostree-repo-checkout.c \
        src/libostree/ostree-repo-libarchive.c \
+       src/libostree/ostree-repo-prune.c \
        src/libostree/ostree-repo-refs.c \
        src/libostree/ostree-repo-traverse.c \
        src/libostree/ostree-repo.h \
index 3962104a7a804e1cc1b778bbc15d04f63fc15a79..8083bf2dbd9eec1615930b8919b122c357b9c3fa 100644 (file)
@@ -22,8 +22,6 @@ bin_PROGRAMS += ostree
 ostree_SOURCES = src/ostree/main.c \
        src/ostree/ostree-curl-fetcher.h \
        src/ostree/ostree-curl-fetcher.c \
-       src/ostree/ostree-prune.h \
-       src/ostree/ostree-prune.c \
        src/ostree/ot-builtin-admin.c \
        src/ostree/ot-builtins.h \
        src/ostree/ot-builtin-cat.c \
diff --git a/src/libostree/ostree-repo-prune.c b/src/libostree/ostree-repo-prune.c
new file mode 100644 (file)
index 0000000..0f173d5
--- /dev/null
@@ -0,0 +1,181 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
+ *
+ * Copyright (C) 2011,2013 Colin Walters <walters@verbum.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ *
+ * Author: Colin Walters <walters@verbum.org>
+ */
+
+#include "config.h"
+
+#include "ostree-repo.h"
+
+typedef struct {
+  OstreeRepo *repo;
+  GHashTable *reachable;
+  guint n_reachable_meta;
+  guint n_reachable_content;
+  guint n_unreachable_meta;
+  guint n_unreachable_content;
+  guint64 freed_bytes;
+} OtPruneData;
+
+static gboolean
+maybe_prune_loose_object (OtPruneData        *data,
+                          OstreeRepoPruneFlags    flags,
+                          const char         *checksum,
+                          OstreeObjectType    objtype,
+                          GCancellable       *cancellable,
+                          GError            **error)
+{
+  gboolean ret = FALSE;
+  gs_unref_variant GVariant *key = NULL;
+  gs_unref_object GFile *objf = NULL;
+
+  key = ostree_object_name_serialize (checksum, objtype);
+
+  objf = ostree_repo_get_object_path (data->repo, checksum, objtype);
+
+  if (!g_hash_table_lookup_extended (data->reachable, key, NULL, NULL))
+    {
+      if (!(flags & OSTREE_REPO_PRUNE_FLAGS_NO_PRUNE))
+        {
+          gs_unref_object GFileInfo *info = NULL;
+
+          if ((info = g_file_query_info (objf, OSTREE_GIO_FAST_QUERYINFO,
+                                         G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
+                                         cancellable, error)) == NULL)
+            goto out;
+
+          if (!gs_file_unlink (objf, cancellable, error))
+            goto out;
+
+          data->freed_bytes += g_file_info_get_size (info);
+        }
+      if (OSTREE_OBJECT_TYPE_IS_META (objtype))
+        data->n_unreachable_meta++;
+      else
+        data->n_unreachable_content++;
+    }
+  else
+    {
+      if (OSTREE_OBJECT_TYPE_IS_META (objtype))
+        data->n_reachable_meta++;
+      else
+        data->n_reachable_content++;
+    }
+
+  ret = TRUE;
+ out:
+  return ret;
+}
+
+gboolean
+ostree_repo_prune (OstreeRepo        *repo,
+                   OstreeRepoPruneFlags   flags,
+                   gint               depth,
+                   gint              *out_objects_total,
+                   gint              *out_objects_pruned,
+                   guint64           *out_pruned_object_size_total,
+                   GCancellable      *cancellable,
+                   GError           **error)
+{
+  gboolean ret = FALSE;
+  GHashTableIter hash_iter;
+  gpointer key, value;
+  gs_unref_hashtable GHashTable *objects = NULL;
+  gs_unref_hashtable GHashTable *all_refs = NULL;
+  gs_free char *formatted_freed_size = NULL;
+  OtPruneData data;
+  gboolean refs_only = flags & OSTREE_REPO_PRUNE_FLAGS_REFS_ONLY;
+
+  memset (&data, 0, sizeof (data));
+
+  data.repo = repo;
+  data.reachable = ostree_repo_traverse_new_reachable ();
+
+  if (refs_only)
+    {
+      if (!ostree_repo_list_refs (repo, NULL, &all_refs,
+                                  cancellable, error))
+        goto out;
+      
+      g_hash_table_iter_init (&hash_iter, all_refs);
+      
+      while (g_hash_table_iter_next (&hash_iter, &key, &value))
+        {
+          const char *checksum = value;
+          
+          if (!ostree_repo_traverse_commit (repo, checksum, depth, data.reachable,
+                                            cancellable, error))
+            goto out;
+        }
+    }
+
+  if (!ostree_repo_list_objects (repo, OSTREE_REPO_LIST_OBJECTS_ALL, &objects,
+                                 cancellable, error))
+    goto out;
+
+  if (!refs_only)
+    {
+      g_hash_table_iter_init (&hash_iter, objects);
+      while (g_hash_table_iter_next (&hash_iter, &key, &value))
+        {
+          GVariant *serialized_key = key;
+          const char *checksum;
+          OstreeObjectType objtype;
+
+          ostree_object_name_deserialize (serialized_key, &checksum, &objtype);
+
+          if (objtype != OSTREE_OBJECT_TYPE_COMMIT)
+            continue;
+          
+          if (!ostree_repo_traverse_commit (repo, checksum, depth, data.reachable,
+                                            cancellable, error))
+            goto out;
+        }
+    }
+
+  g_hash_table_iter_init (&hash_iter, objects);
+  while (g_hash_table_iter_next (&hash_iter, &key, &value))
+    {
+      GVariant *serialized_key = key;
+      GVariant *objdata = value;
+      const char *checksum;
+      OstreeObjectType objtype;
+      gboolean is_loose;
+      
+      ostree_object_name_deserialize (serialized_key, &checksum, &objtype);
+      g_variant_get_child (objdata, 0, "b", &is_loose);
+
+      if (!is_loose)
+        continue;
+
+      if (!maybe_prune_loose_object (&data, flags, checksum, objtype,
+                                     cancellable, error))
+        goto out;
+    }
+  ret = TRUE;
+  *out_objects_total = (data.n_reachable_meta + data.n_unreachable_meta +
+                        data.n_reachable_content + data.n_unreachable_content);
+  *out_objects_pruned = (data.n_unreachable_meta + data.n_unreachable_content);
+  *out_pruned_object_size_total = data.freed_bytes;
+ out:
+  if (data.reachable)
+    g_hash_table_unref (data.reachable);
+  return ret;
+}
index 8b5b55b0fb8d0218d44a276320259b864a1cfb41..998bf43360e37877053e83cde4a03d77284bb772 100644 (file)
@@ -332,6 +332,20 @@ gboolean ostree_repo_traverse_commit (OstreeRepo         *repo,
                                       GCancellable       *cancellable,
                                       GError            **error);
 
+typedef enum {
+  OSTREE_REPO_PRUNE_FLAGS_NONE,
+  OSTREE_REPO_PRUNE_FLAGS_NO_PRUNE,
+  OSTREE_REPO_PRUNE_FLAGS_REFS_ONLY
+} OstreeRepoPruneFlags;
+
+gboolean ostree_repo_prune (OstreeRepo        *repo,
+                            OstreeRepoPruneFlags   flags,
+                            gint               depth,
+                            gint              *out_objects_total,
+                            gint              *out_objects_pruned,
+                            guint64           *out_pruned_object_size_total,
+                            GCancellable      *cancellable,
+                            GError           **error);
 
 G_END_DECLS
 
diff --git a/src/ostree/ostree-prune.c b/src/ostree/ostree-prune.c
deleted file mode 100644 (file)
index 741e6b8..0000000
+++ /dev/null
@@ -1,181 +0,0 @@
-/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
- *
- * Copyright (C) 2011 Colin Walters <walters@verbum.org>
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
- *
- * Author: Colin Walters <walters@verbum.org>
- */
-
-#include "config.h"
-
-#include "ostree-prune.h"
-
-typedef struct {
-  OstreeRepo *repo;
-  GHashTable *reachable;
-  guint n_reachable_meta;
-  guint n_reachable_content;
-  guint n_unreachable_meta;
-  guint n_unreachable_content;
-  guint64 freed_bytes;
-} OtPruneData;
-
-static gboolean
-maybe_prune_loose_object (OtPruneData        *data,
-                          OstreePruneFlags    flags,
-                          const char         *checksum,
-                          OstreeObjectType    objtype,
-                          GCancellable       *cancellable,
-                          GError            **error)
-{
-  gboolean ret = FALSE;
-  gs_unref_variant GVariant *key = NULL;
-  gs_unref_object GFile *objf = NULL;
-
-  key = ostree_object_name_serialize (checksum, objtype);
-
-  objf = ostree_repo_get_object_path (data->repo, checksum, objtype);
-
-  if (!g_hash_table_lookup_extended (data->reachable, key, NULL, NULL))
-    {
-      if (!(flags & OSTREE_PRUNE_FLAGS_NO_PRUNE))
-        {
-          gs_unref_object GFileInfo *info = NULL;
-
-          if ((info = g_file_query_info (objf, OSTREE_GIO_FAST_QUERYINFO,
-                                         G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
-                                         cancellable, error)) == NULL)
-            goto out;
-
-          if (!gs_file_unlink (objf, cancellable, error))
-            goto out;
-
-          data->freed_bytes += g_file_info_get_size (info);
-        }
-      if (OSTREE_OBJECT_TYPE_IS_META (objtype))
-        data->n_unreachable_meta++;
-      else
-        data->n_unreachable_content++;
-    }
-  else
-    {
-      if (OSTREE_OBJECT_TYPE_IS_META (objtype))
-        data->n_reachable_meta++;
-      else
-        data->n_reachable_content++;
-    }
-
-  ret = TRUE;
- out:
-  return ret;
-}
-
-gboolean
-ostree_prune (OstreeRepo        *repo,
-              OstreePruneFlags   flags,
-              gint               depth,
-              gint              *out_objects_total,
-              gint              *out_objects_pruned,
-              guint64           *out_pruned_object_size_total,
-              GCancellable      *cancellable,
-              GError           **error)
-{
-  gboolean ret = FALSE;
-  GHashTableIter hash_iter;
-  gpointer key, value;
-  gs_unref_hashtable GHashTable *objects = NULL;
-  gs_unref_hashtable GHashTable *all_refs = NULL;
-  gs_free char *formatted_freed_size = NULL;
-  OtPruneData data;
-  gboolean refs_only = flags & OSTREE_PRUNE_FLAGS_REFS_ONLY;
-
-  memset (&data, 0, sizeof (data));
-
-  data.repo = repo;
-  data.reachable = ostree_repo_traverse_new_reachable ();
-
-  if (refs_only)
-    {
-      if (!ostree_repo_list_refs (repo, NULL, &all_refs,
-                                  cancellable, error))
-        goto out;
-      
-      g_hash_table_iter_init (&hash_iter, all_refs);
-      
-      while (g_hash_table_iter_next (&hash_iter, &key, &value))
-        {
-          const char *checksum = value;
-          
-          if (!ostree_repo_traverse_commit (repo, checksum, depth, data.reachable,
-                                            cancellable, error))
-            goto out;
-        }
-    }
-
-  if (!ostree_repo_list_objects (repo, OSTREE_REPO_LIST_OBJECTS_ALL, &objects,
-                                 cancellable, error))
-    goto out;
-
-  if (!refs_only)
-    {
-      g_hash_table_iter_init (&hash_iter, objects);
-      while (g_hash_table_iter_next (&hash_iter, &key, &value))
-        {
-          GVariant *serialized_key = key;
-          const char *checksum;
-          OstreeObjectType objtype;
-
-          ostree_object_name_deserialize (serialized_key, &checksum, &objtype);
-
-          if (objtype != OSTREE_OBJECT_TYPE_COMMIT)
-            continue;
-          
-          if (!ostree_repo_traverse_commit (repo, checksum, depth, data.reachable,
-                                            cancellable, error))
-            goto out;
-        }
-    }
-
-  g_hash_table_iter_init (&hash_iter, objects);
-  while (g_hash_table_iter_next (&hash_iter, &key, &value))
-    {
-      GVariant *serialized_key = key;
-      GVariant *objdata = value;
-      const char *checksum;
-      OstreeObjectType objtype;
-      gboolean is_loose;
-      
-      ostree_object_name_deserialize (serialized_key, &checksum, &objtype);
-      g_variant_get_child (objdata, 0, "b", &is_loose);
-
-      if (!is_loose)
-        continue;
-
-      if (!maybe_prune_loose_object (&data, flags, checksum, objtype,
-                                     cancellable, error))
-        goto out;
-    }
-  ret = TRUE;
-  *out_objects_total = (data.n_reachable_meta + data.n_unreachable_meta +
-                        data.n_reachable_content + data.n_unreachable_content);
-  *out_objects_pruned = (data.n_unreachable_meta + data.n_unreachable_content);
-  *out_pruned_object_size_total = data.freed_bytes;
- out:
-  if (data.reachable)
-    g_hash_table_unref (data.reachable);
-  return ret;
-}
diff --git a/src/ostree/ostree-prune.h b/src/ostree/ostree-prune.h
deleted file mode 100644 (file)
index 882fb60..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
- *
- * Copyright (C) 2013 Colin Walters <walters@verbum.org>
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License as published
- * by the Free Software Foundation; either version 2 of the licence or (at
- * your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General
- * Public License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
- * Boston, MA 02111-1307, USA.
- */
-
-#pragma once
-
-#include "ostree.h"
-
-G_BEGIN_DECLS
-
-typedef enum {
-  OSTREE_PRUNE_FLAGS_NONE,
-  OSTREE_PRUNE_FLAGS_NO_PRUNE,
-  OSTREE_PRUNE_FLAGS_REFS_ONLY
-} OstreePruneFlags;
-
-gboolean ostree_prune (OstreeRepo        *repo,
-                       OstreePruneFlags   flags,
-                       gint               depth,
-                       gint              *out_objects_total,
-                       gint              *out_objects_pruned,
-                       guint64           *out_pruned_object_size_total,
-                       GCancellable      *cancellable,
-                       GError           **error);
-
-G_END_DECLS
-
index 1ca242a73befe7bbcb8c4ff7d9dc0c5cd15399d8..d8d9e8e32e4bc5e276c7e6baa5b38d734ea0c811 100644 (file)
@@ -27,8 +27,7 @@
 #include "ot-deployment.h"
 #include "ot-config-parser.h"
 #include "otutil.h"
-#include "ostree-core.h"
-#include "ostree-prune.h"
+#include "ostree.h"
 #include "libgsystem.h"
 
 static gboolean
@@ -463,9 +462,9 @@ generate_deployment_refs_and_prune (GFile               *sysroot,
         goto out;
     }
 
-  if (!ostree_prune (repo, OSTREE_PRUNE_FLAGS_REFS_ONLY, 0,
-                     &n_objects_total, &n_objects_pruned, &freed_space,
-                     cancellable, error))
+  if (!ostree_repo_prune (repo, OSTREE_REPO_PRUNE_FLAGS_REFS_ONLY, 0,
+                          &n_objects_total, &n_objects_pruned, &freed_space,
+                          cancellable, error))
     goto out;
   if (freed_space > 0)
     {
index 1b469ebe34feffd710a0e08718c6ac55a05bd038..3780c2a91520c16396e479c4c3c5b4949b4c783a 100644 (file)
@@ -28,8 +28,7 @@
 #include "ot-config-parser.h"
 #include "ot-bootloader-syslinux.h"
 #include "otutil.h"
-#include "ostree-core.h"
-#include "ostree-prune.h"
+#include "ostree.h"
 #include "libgsystem.h"
 
 OtOrderedHash *
index f52153cbe005ed4605a7255116dd0041b1b5a9f5..3c0a68c7a8b380eed1e602d6151f8dc046e64044 100644 (file)
@@ -23,7 +23,7 @@
 #include "config.h"
 
 #include "ot-builtins.h"
-#include "ostree-prune.h"
+#include "ostree.h"
 
 #include <glib/gi18n.h>
 #include <glib/gprintf.h>
@@ -47,7 +47,7 @@ ostree_builtin_prune (int argc, char **argv, GFile *repo_path, GError **error)
   GCancellable *cancellable = NULL;
   gs_unref_object OstreeRepo *repo = NULL;
   gs_free char *formatted_freed_size = NULL;
-  OstreePruneFlags pruneflags = 0;
+  OstreeRepoPruneFlags pruneflags = 0;
   gint n_objects_total;
   gint n_objects_pruned;
   guint64 objsize_total;
@@ -63,13 +63,13 @@ ostree_builtin_prune (int argc, char **argv, GFile *repo_path, GError **error)
     goto out;
 
   if (opt_refs_only)
-    pruneflags |= OSTREE_PRUNE_FLAGS_REFS_ONLY;
+    pruneflags |= OSTREE_REPO_PRUNE_FLAGS_REFS_ONLY;
   if (opt_no_prune)
-    pruneflags |= OSTREE_PRUNE_FLAGS_NO_PRUNE;
+    pruneflags |= OSTREE_REPO_PRUNE_FLAGS_NO_PRUNE;
 
-  if (!ostree_prune (repo, pruneflags, opt_depth,
-                     &n_objects_total, &n_objects_pruned, &objsize_total,
-                     cancellable, error))
+  if (!ostree_repo_prune (repo, pruneflags, opt_depth,
+                          &n_objects_total, &n_objects_pruned, &objsize_total,
+                          cancellable, error))
     goto out;
 
   formatted_freed_size = g_format_size_full (objsize_total, 0);
@@ -77,7 +77,7 @@ ostree_builtin_prune (int argc, char **argv, GFile *repo_path, GError **error)
   g_print ("Total objects: %u\n", n_objects_total);
   if (n_objects_pruned == 0)
     g_print ("No unreachable objects\n");
-  else if (pruneflags & OSTREE_PRUNE_FLAGS_NO_PRUNE)
+  else if (pruneflags & OSTREE_REPO_PRUNE_FLAGS_NO_PRUNE)
     g_print ("Would delete: %u objects, freeing %s bytes\n",
              n_objects_pruned, formatted_freed_size);
   else